package cn.com.server.controller.userManage;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;

import cn.com.server.common.AjaxCodeUtil;
import cn.com.server.common.AjaxObject;
import cn.com.server.service.RoleResourceService;
import cn.com.server.service.RoleService;
import cn.com.server.extend.model.RoleEntityModel;
import cn.com.server.extend.model.RoleModel;
import cn.com.server.extend.view.RoleView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author qing
 * @since 2017-11-20
 */
@Controller
@RequestMapping("/userManage/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private RoleResourceService roleResourceService;
	
	@RequestMapping("/roleList")
	public String roleList(HttpServletRequest request, HttpServletResponse response) {
		return "/userManager/roleList";
	}
	
	@RequestMapping("/getRoleList")
	@ResponseBody
	public AjaxObject getRoleList(RoleModel model,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		Page<RoleView> pageData = roleService.selectRoleViewList(model);
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(pageData);
		return ajaxObject;
	}
	
	@RequestMapping("/getRoleResourceList")
	@ResponseBody
	public AjaxObject getRoleResourceList(Long roleId,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(roleResourceService.selectRoleResourceListByRoleId(roleId));
		return ajaxObject;
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public AjaxObject save(RoleEntityModel model,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		if(model.getId()==null) {
			roleService.insertRoleAndRoleGroup(model);
		}else {
			roleService.update(model);
		}
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		return ajaxObject;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxObject deleteUpdate(String ids,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		try {
			if(roleService.deleteRoleAndAllByRoleIds(ids)>0) {
				return AjaxCodeUtil.getAjaxObject("success");
			}
			return AjaxCodeUtil.getAjaxObject("deleteException");
		}catch (Exception e) {
			e.printStackTrace();
			return AjaxCodeUtil.getAjaxObject("deleteException");
		}
	}
	
	@RequestMapping("/saveRoleResource")
	@ResponseBody
	public AjaxObject saveRoleResource(Long roleId,String resourceIds,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		try {
			if(roleId!=null) {
				if(roleResourceService.insertAndUpdateRoleResource(resourceIds, roleId)>0) {
					return AjaxCodeUtil.getAjaxObject("success");
				}else {
					return AjaxCodeUtil.getAjaxObject("failed");
				}
			}else {
				return AjaxCodeUtil.getAjaxObject("failed");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return AjaxCodeUtil.getAjaxObject("failed");
		}
	}
}
